import Taro, {Component} from '@tarojs/taro'
import {View, Button} from '@tarojs/components'
import {connect} from '@tarojs/redux'
import {getUserInfo} from '../../actions/user'
import Head from '../../components/head/head'
import Panel from '../../components/user/panel'

import './user.scss'

@connect((store) => {
  return {...store.user}
})

class User extends Component {
  state = {
    recent_topics: [],
    recent_replies: []
  }
  toCreate () {
    Taro.navigateTo({url: '/pages/publish/publish'})
  }
  componentWillMount () {
    let params = {loginname: this.props.loginname}
    getUserInfo(params).then(res => {
      if(res && res.data) {
        this.setState({
          recent_topics: res.data.recent_topics,
          recent_replies: res.data.recent_replies
        })
      }
    })
  }
  render () {
    const {loginname, avatar_url} = this.props
    const {recent_topics, recent_replies} = this.state
    return (
      <View>
        <Head loginname={loginname} avatar_url={avatar_url} />
        <Panel listData={recent_topics} title='最近发布的话题' />
        <Panel listData={recent_replies} title='最近回复的话题' />
        <Button type='primary' onClick={this.toCreate.bind(this)}>发布话题</Button>
      </View>
    )
  }
}

export default User